<template>
  <div>
    <div
      class="search-bar"
      :class="{ 'hide-title': !titleVisible, 'hide-shadow': !shadowVisible }"
    >
      <Transition name="title-move">
        <div class="search-bar-title-wrapper" v-show="titleVisible">
          <!-- 标题:书城 -->
          <div class="title-text-back-wrapper">
            <span class="title-text title">{{ $t('home.title') }}</span>
          </div>
          <!-- 反正是一个手机振动的图标 -->
          <div class="title-icon-shake-wrapper" @click="showFlapCard">
            <span class="icon-shake icon"></span>
          </div>
        </div>
      </Transition>
      <!-- 返回按钮 -->
      <div
        class="title-icon-back-wrapper"
        :class="{ 'hide-title': !titleVisible }"
        @click="back"
      >
        <span class="icon-back icon"></span>
      </div>

      <!-- input框 -->
      <div
        class="search-bar-input-wrapper"
        :class="{ 'hide-title': !titleVisible }"
      >
        <!-- 占位DOM -->
        <div
          class="search-bar-blank"
          :class="{ 'hide-title': !titleVisible }"
        ></div>
        <!-- 搜索框 -->
        <div class="search-bar-input">
          <span class="icon-search icon"></span>
          <input
            type="text"
            class="input"
            :placeholder="$t('home.hint')"
            v-model="searchText"
            @click="showHotSearch"
            @keyup.13.exact="search"
          />
        </div>
      </div>
    </div>
    <HotSearchList v-show="hotSearchVisible" ref="hotSearch"></HotSearchList>
  </div>
</template>

<script>
import { storeHomeMixin } from '@/utils/mixin';
import HotSearchList from './HotSearchList.vue';

export default {
  mixins: [storeHomeMixin],
  components: {
    HotSearchList,
  },
  data() {
    return {
      searchText: '',
      titleVisible: true,
      shadowVisible: false,
      hotSearchVisible: false,
    };
  },
  watch: {
    offsetY(offsetY) {
      if (offsetY > 0) {
        this.hideTitle();
        this.showShadow();
      } else {
        this.showTitle();
        this.hideShadow();
      }
    },
    hotSearchOffsetY(offsetY) {
      if (offsetY > 0) {
        this.showShadow();
      } else {
        this.hideShadow();
      }
    },
  },
  methods: {
    // *输入框按下回车键触发搜索
    search() {
      this.$router.push({
        path: '/store/list',
        query: {
          keyword: this.searchText,
        },
      });
    },

    // *点击手机振动图标触发
    showFlapCard() {
      this.setFlapCardVisible(true);
    },

    // *点击返回图标触发
    back() {
      if (this.offsetY > 0) {
        this.showShadow();
      } else {
        this.hideShadow();
      }
      if (this.hotSearchVisible) {
        this.hideHotSearch();
      } else {
        this.$router.push('/store/shelf');
      }
    },

    // *点击搜索栏执行
    showHotSearch() {
      this.hideTitle();
      this.hideShadow();
      this.hotSearchVisible = true;
      this.$nextTick(() => {
        this.$refs.hotSearch.reset();
      });
    },
    hideHotSearch() {
      this.hotSearchVisible = false;
      if (this.offsetY > 0) {
        this.hideTitle();
        this.showShadow();
      } else {
        this.showTitle();
        this.hideShadow();
      }
    },

    // *操作标题
    hideTitle() {
      this.titleVisible = false;
    },
    showTitle() {
      this.titleVisible = true;
    },

    // *操作阴影
    hideShadow() {
      this.shadowVisible = false;
    },
    showShadow() {
      this.shadowVisible = true;
    },
  },
};
</script>

<style scoped lang="scss">
@import '../../assets/styles/global';
.search-bar {
  width: 100%;
  position: relative;
  height: px2rem(94);
  z-index: 150;
  box-shadow: 0 px2rem(2) px2rem(2) 0 rgba(0, 0, 0, 0.1);
  &.hide-title {
    height: px2rem(52);
  }
  &.hide-shadow {
    box-shadow: none;
  }

  .search-bar-title-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: px2rem(42);

    .title-text-back-wrapper {
      width: 100%;
      height: px2rem(42);
      @include center;
    }
    .title-icon-shake-wrapper {
      position: absolute;
      right: px2rem(15);
      top: 0;
      height: px2rem(42);
      @include center;
    }
  }

  .title-icon-back-wrapper {
    position: absolute;
    left: px2rem(15);
    top: 0;
    height: px2rem(42);
    transition: height $animationTime $animationType;
    z-index: 200;
    @include center;
    &.hide-title {
      height: px2rem(52);
    }
  }

  .search-bar-input-wrapper {
    position: absolute;
    left: 0;
    top: px2rem(42);
    display: flex;
    width: 100%;
    height: px2rem(52);
    padding: px2rem(10);
    box-sizing: border-box;
    transition: top $animationTime $animationType;
    &.hide-title {
      top: 0;
    }

    .search-bar-blank {
      flex: 0 0 0;
      width: 0;
      transition: all $animationTime $animationType;
      &.hide-title {
        flex: 0 0 px2rem(31);
        width: px2rem(31);
      }
    }

    .search-bar-input {
      flex: 1;
      width: 100%;
      border-radius: px2rem(20);
      background: #f4f4f4;
      padding: px2rem(5) px2rem(15);
      box-sizing: border-box;
      border: px2rem(1) solid #eee;
      @include left;
      .icon-search {
        color: #999;
        font-size: 35%;
      }
      .input {
        width: 100%;
        border: none;
        background: transparent;
        margin-left: px2rem(10);
        font-size: px2rem(12);
        color: #666;
        &:focus {
          outline: none;
        }

        &::-webkit-input-placeholder {
          color: #ccc;
        }
      }
    }
  }
}
</style>
